# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from dao import models

# Create your views here.


def get_derction(request):
	derction_queryset = models.Dirction.objects.all().values('id', 'name')

	return derction_queryset


def get_classification(request, current_url, direction_id, classification_id):
	if int(direction_id) == 0:  # 方向选中‘全部’时
		classification_queryset = models.Classification.objects.all().values('id', 'name')
	else:  # 方向选中某一个时
		obj_direction = models.Dirction.objects.get(id=int(direction_id))  # 查询某方向
		classification_queryset = obj_direction.classification.all().values('id', 'name')  # 查询某方向下的分类

	classification_id_list = list(map(lambda x: x['id'], classification_queryset))  # 分类id列表

	# 判断url中分类id是否存在于所查询的方向下（方向与分类为多对多关系）--方向非全部分类非全部时需处理
	# 若存在则不做处理，即还处于选中状态
	# 若不存在则将原选中的分类改为‘全部’，即方向id为0
	if int(classification_id) in classification_id_list:
		pass
	else:
		url_part_list = current_url.split('-')
		url_part_list[2] = str(0)
		current_url = '-'.join(url_part_list)

	return classification_queryset, current_url


def get_level(request):
	# level_list = models.Video.level_choice  # 结果为元组
	level_list = list(map(lambda x: {'id': x[0], 'name': x[1]}, models.Video.level_choice))  # 使用map将元组构造为字典列表

	return level_list

def get_video(request, **q):
	video_queryset = models.Video.objects.filter(**q).values('title', 'summary', 'img', 'href')

	return video_queryset